/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tchepannou.limbe.test;


import com.tchepannou.limbe.Service;
import com.tchepannou.limbe.engine.Engine;
import com.tchepannou.limbe.engine.EngineConfigurator;
import com.tchepannou.limbe.mock.servlet.MockServletContext;
import junit.framework.TestCase;

/**
 *
 * @author herve
 */
public class BaseTestCase
    extends TestCase
{
    //-- Static Attribute
    private static long _uid = System.currentTimeMillis ();

    //-- Attribute
    private MockServletContext _servletContext;
    private Engine _engine;



    //-- Constructor
    public BaseTestCase ()
    {
    }

    public BaseTestCase (String name)
    {
        super (name);
    }

    //-- TestCase override
    @Override
    protected void setUp ()
        throws Exception
    {
        System.out.println ("--- " + getClass().getSimpleName() + "." + getName () + "--------------------------------");

        _servletContext = createServletContext ();

        _engine = Engine.getInstance ();
        new EngineConfigurator ().configure (_engine, _servletContext);
        _engine.init (_servletContext);
    }

    @Override
    protected void tearDown ()
        throws Exception
    {
        System.out.println ("");
        
        _engine.destroy ();
    }

    //-- Protected
    protected synchronized long nextUID ()
    {
        return ++_uid;
    }

    //-- Protected
    protected MockServletContext createServletContext ()
    {
        return new MockServletContext ( new MockServletContext.ClasspathResourceLocator ());
    }
    
    protected void register (Class<? extends Service> spec, Service service)
    {
        _engine.registerService (spec, service);
        service.init (_engine);
    }

    protected Service findService (Class<? extends Service> spec)
    {
        return _engine.findService (spec);
    }

    protected MockServletContext getServletContext ()
    {
        return _servletContext;
    }

    protected Engine getEngine ()
    {
        return _engine;
    }
}
